package moni.anyou.com.view.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import moni.anyou.com.view.R;

/**
 * Created by lubiao on 2016/10/30.
 */
public class PopDealwithMsg extends PopupWindow {

    private TextView tvReceive;
    private TextView tvReject;
    private TextView tv_pop_title;
    private TextView tv_pop_contents;
    private Button btnCancle;
    private View mView;
    Activity mContext;

    public PopDealwithMsg(Activity context, View.OnClickListener itemsOnClick,String title,String contents) {
        super(context);
        mContext=context;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mView = mInflater.inflate(R.layout.pop_dealwithmsg,null);
        tvReceive=(TextView) mView.findViewById(R.id.tvReceive);
        tvReject=(TextView) mView.findViewById(R.id.tvReject);
        tv_pop_title=(TextView) mView.findViewById(R.id.tv_pop_title);
        tv_pop_contents=(TextView) mView.findViewById(R.id.tv_pop_contents);
        tv_pop_title.setText(title);
        tv_pop_contents.setText(contents);
        btnCancle=(Button) mView.findViewById(R.id.btn_cancel);
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tvReceive.setOnClickListener(itemsOnClick);
        tvReject.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        backgroundAlpha(0.5f);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

        this.setOnDismissListener(new poponDismissListener());
    }


    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp =mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }


    class poponDismissListener implements OnDismissListener{

        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }
}
